<template>
  <div style="height: 100%" class="relative">
    <el-card
      class="!border-none el-card-client"
      v-loading="pager.loading"
      shadow="never"
      style="height: calc(100vh - 7.5rem); overflow-y: auto"
    >
      <template #header>
        <el-row :gutter="10">
          <el-col :span="5">
            <el-input
              class="mr_10 input_style"
              v-model="queryParams.title"
              clearable
              placeholder="请输入名称"
            />
          </el-col>
          <el-col :span="19">
            <el-button
              @click="resetParams"
              color="#63ccba"
              type="success"
              class="btn_style mr_10"
            >
              <template #icon>
                <el-icon color="#fff">
                  <Search />
                </el-icon>
              </template>
              搜索
            </el-button>
            <el-button
              v-perms="['activity.zg_apply/add']"
              type="primary"
              @click="handleAdd"
            >
              <template #icon>
                <icon name="el-icon-Plus" />
              </template>
              新增礼物
            </el-button>
          </el-col>
          <!-- <el-col :span="2" class="flex justify-end">
            <move-button></move-button>
          </el-col> -->
        </el-row>
      </template>
      <el-table
        :data="pager.lists"
        border
        @selection-change="handleSelectionChange"
        v-loading="pager.loading"
        stripe
      >
        <!-- 
        max-height="calc(100vh - 16rem)"
        <el-table-column type="selection" width="55" align="center" /> -->
        <!-- <el-table-column label="id" prop="id" show-overflow-tooltip align="center" /> -->
        <el-table-column
          label="名称"
          prop="name"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column label="图片" prop="status" align="center">
          <template #default="{ row }">
            <div class="demo-image__preview">
              <el-image
                style="width: 100px; height: 100px"
                :src="row.image"
                :preview-src-list="[row.image]"
                :initial-index="0"
                fit="cover"
                hide-on-click-modal="true"
                preview-teleported="true"
              />
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="添加时间"
          prop="create_time"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column label="操作" width="150" align="center">
          <template #default="{ row }">
            <div class="flex">
              <el-button
                v-perms="['member.member/edit']"
                type="primary"
                @click="handleEdit(row)"
                style="width: 50px"
                size="small"
                class="btn_style mr_10"
              >
                <template #icon>
                  <el-icon>
                    <Edit />
                  </el-icon> </template
                >编辑
              </el-button>
              <el-popconfirm title="确定删除吗？" @confirm="deleteOnce(row.id)">
                <template #reference>
                  <el-button
                    v-perms="['member.member/delete']"
                    type="danger"
                    style="width: 50px"
                    size="small"
                    class="btn_style"
                  >
                    <template #icon>
                      <el-icon>
                        <Delete />
                      </el-icon>
                    </template>
                    删除
                  </el-button>
                </template>
              </el-popconfirm>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="position">
        <pagination v-model="pager" @change="getLists" />
      </div>
    </el-card>
    <edit-popup
      v-if="showEdit"
      ref="editRef"
      :dict-data="dictData"
      @success="getLists"
      @close="showEdit = false"
    />
  </div>
</template>

<script lang="ts" setup name="zgApplyLists">
import { usePaging } from "@/hooks/usePaging";
import { useDictData } from "@/hooks/useDictOptions";
import { apiZgGiftLists, apiZgGiftDelete } from "@/api/zg_gift";
import { timeFormat } from "@/utils/util";
import feedback from "@/utils/feedback";
import EditPopup from "./edit.vue";
import moveButton from "@/components/moveButton/index.vue";

const srcList = ref([
  "https://fuss10.elemecdn.com/a/3f/3302e58f9a181d2509f3dc0fa68b0jpeg.jpeg",
]);

const editRef = shallowRef<InstanceType<typeof EditPopup>>();
// 是否显示编辑框
const showEdit = ref(false);

// 查询条件
const queryParams = reactive({
  title: "",
  type: "",
  status: "0",
});
const getStatus = (row) => {};
// 选中数据
const selectData = ref<any[]>([]);

// 表格选择后回调事件
const handleSelectionChange = (val: any[]) => {
  selectData.value = val.map(({ id }) => id);
};

// 获取字典数据
const { dictData } = useDictData("sh_status,sh_type");

// 分页相关
const { pager, getLists, resetParams, resetPage } = usePaging({
  fetchFun: apiZgGiftLists,
  params: queryParams,
});

// 添加
const handleAdd = async () => {
  showEdit.value = true;
  await nextTick();
  editRef.value?.open("add");
};

// 编辑
const handleEdit = async (data: any) => {
  showEdit.value = true;
  await nextTick();
  editRef.value?.open("edit");
  editRef.value?.setFormData(data);
};

// 删除
const handleDelete = async (id: number | any[]) => {
  await feedback.confirm("确定要删除？");
  await apiZgGiftDelete({ id });
  getLists();
};
const deleteOnce = async (id: number | any[]) => {
  //   await feedback.confirm("确定要删除？")
  await apiZgGiftDelete({ id });
  getLists();
};
getLists();
</script>
<style lang="scss" scoped>
.flex {
  display: flex !important;
}

.btn_style {
  color: #fff;
}
// .position {
//   position: fixed;
//   bottom: 10px;
//   left: 50px;
// }
</style>
